package org.dromara.visitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.visitor.domain.dto.OrgManageAddReq;
import org.dromara.visitor.domain.dto.OrgManageImportExcel;
import org.dromara.visitor.domain.dto.OrgManageQueryReq;
import org.dromara.visitor.domain.dto.OrgManageResp;
import org.dromara.visitor.domain.entity.OrgManage;
import org.dromara.visitor.domain.entity.Organization;
import org.dromara.visitor.mapper.OrgManageMapper;
import org.dromara.visitor.mapper.OrganizationMapper;
import org.dromara.visitor.service.OrgManageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class OrgManageServiceImpl extends ServiceImpl<OrgManageMapper, OrgManage> implements OrgManageService {
    @Resource
    private OrgManageMapper orgManageMapper;

    @Resource
    private OrganizationMapper organizationMapper;
    @Override
    public void add(OrgManageAddReq addReq) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        LocalDateTime now = LocalDateTime.now();
        //新增
        LambdaQueryWrapper<OrgManage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrgManage::getCardNum,addReq.getCardNum());
        if(addReq.getId() != null){
            wrapper.notIn(OrgManage::getId,addReq.getId());
        }
        OrgManage exist = this.getOne(wrapper);
        if(exist != null){
            throw new ServiceException("该证件号码已存在");
        }
        if(addReq.getId() == null){
            OrgManage orgManage1 = new OrgManage();
            orgManage1.setApplyPhone(addReq.getApplyPhone());
            orgManage1.setApplyName(addReq.getApplyName());
            orgManage1.setCardNum(addReq.getCardNum());
            orgManage1.setOrgId(addReq.getOrgId());
            orgManage1.setCreateBy(loginUser.getUserId());
            orgManage1.setCreateTime(now);
            orgManage1.setUpdateBy(loginUser.getUserId());
            orgManage1.setUpdateTime(now);
            this.save(orgManage1);
        }else{
            //修改
            OrgManage orgManage = this.getById(addReq.getId());
            if(orgManage == null){
                throw new ServiceException("该记录不存在");
            }
            orgManage.setApplyPhone(addReq.getApplyPhone());
            orgManage.setApplyName(addReq.getApplyName());
            orgManage.setCardNum(addReq.getCardNum());
            orgManage.setOrgId(addReq.getOrgId());
            orgManage.setUpdateBy(loginUser.getUserId());
            orgManage.setUpdateTime(now);
            this.updateById(orgManage);
        }
    }

    @Override
    public Page<OrgManageResp> listPage(OrgManageQueryReq queryReq) {
        Page page = new Page(queryReq.getPageNum(), queryReq.getPageSize());
        List<OrgManageResp> list = orgManageMapper.listPage(page,queryReq);
        page.setRecords(list);
        return page;
    }

    @Override
    public void importExcel(List<OrgManageImportExcel> list) {
        //对组织进行校验
        List<String> orgNames = list.stream().map(OrgManageImportExcel::getOrgName).distinct().collect(Collectors.toList());
        List<String> cardNums = list.stream().map(OrgManageImportExcel::getCardNum).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<Organization> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Organization::getOrgName,orgNames);
        LambdaQueryWrapper<OrgManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(OrgManage::getApplyPhone,cardNums);
        Map<String,Organization> orgMap = organizationMapper.selectList(wrapper).stream().collect(Collectors.toMap(Organization::getOrgName, Function.identity()));
        Map<String,OrgManage> cardNumMap = orgManageMapper.selectList(queryWrapper).stream().collect(Collectors.toMap(OrgManage::getCardNum,Function.identity()));
        int i = 1;
        List<OrgManage> orgManageList = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        LoginUser loginUser = LoginHelper.getLoginUser();
        for(OrgManageImportExcel orgManageImportExcel : list){
            i = i+1;
            if(!orgMap.containsKey(orgManageImportExcel.getOrgName())){
                throw new ServiceException("第"+i+"行组织名称不存在");
            }
            OrgManage orgManage = new OrgManage();
            BeanUtils.copyProperties(orgManageImportExcel,orgManage);
            if(cardNumMap.containsKey(orgManageImportExcel.getCardNum())){
                orgManage.setId(cardNumMap.get(orgManageImportExcel.getCardNum()).getId());
            }
            orgManage.setOrgId(orgMap.get(orgManageImportExcel.getOrgName()).getId());
            orgManage.setCreateTime(now);
            orgManage.setUpdateTime(now);
            orgManage.setCreateBy(loginUser.getUserId());
            orgManage.setUpdateBy(loginUser.getUserId());
            orgManageList.add(orgManage);
        }
        this.saveOrUpdateBatch(orgManageList);

    }

    @Override
    public List<OrgManageResp> listExport(OrgManageQueryReq queryReq) {
        List<OrgManageResp> list = orgManageMapper.listPage(null,queryReq);
        return list;
    }
}
